package com.lxy.thread;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.lxy.data_bean.PM2_5_bean;
import com.lxy.util.MyUrl;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class Thread_PM2_5 extends Thread{
	private Handler h;
	private String url;
	public Thread_PM2_5(String url,Handler h){
		this.h=h;
		this.url=url;
	}
	@Override
	public void run() {
		JSONArray jArray=null;
		JSONObject json=null;
		String data=MyUrl.getDocumentAt(url);
		System.out.println("data-->"+data);
		////////////////////////////////////////
		try {
			if(data.startsWith("{")){
				JSONObject jtest=new JSONObject(data);
				String s=jtest.getString("error");
				if(s!=null){
					Log.i("error", s);
					return;
				}
			}
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		////////////////////////////////////////
		try {
			jArray=new JSONArray(data);
			json=(JSONObject) jArray.get(jArray.length()-1);
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		PM2_5_bean pm=new PM2_5_bean();
		try {
			pm.setAqi(json.getString("aqi"));
			pm.setPm2_5(json.getString("pm2_5"));
			pm.setPm2_5_24h(json.getString("pm2_5_24h"));
			pm.setPrimary_pollutant(json.getString("primary_pollutant"));
			pm.setQuality(json.getString("quality"));
			pm.setTime_point(json.getString("time_point"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		Log.i("info:",pm.toString());
		Message msg=Message.obtain();
		msg.what=2;
		msg.obj=pm;
		h.sendMessage(msg);
	}
	
}
